///////////////////////////////////////////////////////////
//  Header.cpp
//  Implementation of the Class Header
//  Created on:      20-Aug-2013 12:06:02 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "Header.h"
#include "Item.h"
#include "DataSchemaXML.h"
using namespace Data;

Header::Header(const String_t& name)
{
  this->m_type = IObject::HEARDER_T;
  this->m_name = name;
  this->m_parent = NULL;
}


Header::Header(const Header& anotherItem)
{
  this->m_type = IObject::HEARDER_T;
  this->m_name = anotherItem.m_name;
  this->m_parent = anotherItem.m_parent;

  int countChilds = anotherItem.CountChilds();
  for(int index = 0; index < countChilds; index++)
  {
    IObject* cloneData = anotherItem.GetChild(index)->Clone();
    this->m_childItems.push_back(cloneData);
    switch(cloneData->GetType())
    {
    case IObject::ITEM_T:
      {
        dynamic_cast<IItem*>(cloneData)->SetParent(this);
      }
      break;
    case IObject::HEARDER_T:
      {
        dynamic_cast<IHeader*>(cloneData)->SetParent(this);
      }
      break;
    default: break;
    }
  }
}


Header::~Header()
{
  std::vector<IObject*>::iterator childItem = this->m_childItems.begin();
  while(childItem != this->m_childItems.end())
  {
    delete (*childItem);
    (*childItem) = NULL;
    childItem++;
  }
  this->m_childItems.clear();

  this->m_parent = NULL;
}


IObject::ObjectType Header::GetType() const 
{

	return this->m_type;
}


String_t Header::GetName() const 
{
	return this->m_name;
}


IObject* Header::GetChild(unsigned int index) const 
{

  return this->m_childItems[index];
}


void Header::AppendChild(IObject* item)
{
  switch(item->GetType())
  {
  case IObject::HEARDER_T:
    {
      IHeader* childHeader = dynamic_cast<IHeader*>(item);
      childHeader->SetParent(this);
    }
    break;
  case IObject::ITEM_T:
    {
      IItem* itemData = dynamic_cast<IItem*>(item);
      itemData->SetParent(this);
    }
    break;
  default: break;
  }
  this->m_childItems.push_back(item);
}


void Header::SetName(const String_t& name)
{
  this->m_name = name;
}


void Header::InsertChild(unsigned int index, IObject* item)
{
  switch(item->GetType())
  {
  case IObject::HEARDER_T:
    {
      IHeader* childHeader = dynamic_cast<IHeader*>(item);
      childHeader->SetParent(this);
    }
    break;
  case IObject::ITEM_T:
    {
      IItem* itemData = dynamic_cast<IItem*>(item);
      itemData->SetParent(this);
    }
    break;
  default: break;
  }
  this->m_childItems.insert(this->m_childItems.begin() + index, item);
}


IObject* Header::RemoveChild(unsigned int index)
{
	return *(this->m_childItems.erase(this->m_childItems.begin() + index));
}


int Header::CountChilds() const 
{
	return (int)this->m_childItems.size();
}


IObject* Header::Clone()
{
  Header* cloneHeader = new Header(*this);
	return cloneHeader;
}


String_t Header::ToXML()
{
  //<Header name="Header1">
  //  <Item name="example">
  //   <Comment>This is a comment.</Comment>
  //   <UseInteger>0</UseInterger>
  //  </Item>
  //</Header>

  String_t xml = _T("<") + DataSchemaXML::HEADER_ELEMENT;
  String_t context = this->m_name;
  replaceTextXML(context);
  if(!this->m_name.empty())
    xml += _T(" ") + DataSchemaXML::NAME_ATTR + _T("=\"") + context + _T("\">");
  else
    xml += _T(">");

  std::vector<IObject*>::iterator childItem = this->m_childItems.begin();
  while(childItem != this->m_childItems.end())
  {
    xml += (*childItem)->ToXML();
    childItem++;
  }

  xml += _T("</") + DataSchemaXML::HEADER_ELEMENT + _T(">");
	return xml;
}

IObject* Header::operator[]( unsigned int index )
{
  return this->m_childItems[index];
}


IObject* Header::GetParent()
{
	return this->m_parent;
}


void Header::SetParent(IObject* parent)
{
  this->m_parent = parent;
}